// pages/collectfollow/collectfollow.js
var util = require('../../utils/util.js');
import { HTTP } from '../../utils/https.js'
let http = new HTTP()
Page({

  /**
   * 页面的初始数据
   */
  data: {
   check:'', //判断是页面
   curshow:null, //控制弹层的index
   page:1,
   limit:10,
   url:String,
   nodata: true,
   datalist:[],
   user_id:'',
   nodate:true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  list: function (page, limit, user_id, more){
    var that = this;
    const data = {
      page: page,
      limit: limit,
      user_id: user_id
    }

    http.request({
      url: that.data.url,
      data: data,
      success: function (res) {
        wx.stopPullDownRefresh()
        var data = res.data.data
        for (var i = 0; i < data.length; i++) {
          data[i].created_at = util.timestampToTime(data[i].created_at);
        } 

        if (!!more) {
          that.setData({
            datalist: that.data.datalist.concat(res.data.data),
            nodate: true
          })

          if (res.data.data.length == 0) {
            that.setData({
              nodata: false
            });
          }
        } else {
          that.setData({
            datalist: res.data.data,
            nodate: true
          })

          if (res.data.data.length == 0) {
            that.setData({
              nodate: false
            });
          }
        }
      }
    })
  },
  onLoad: function (options) {
    this.setData({
      check:options.name,
      user_id: options.id
    })
  },
  onShow: function () {
    var name = this.data.check;
    var title = '';
    if (name == 'demand_number') {
      title = '我的发布'
      this.setData({
        url: 'mini/v1/personal/my_demand'
      })
    } else if (name == 'me_collect') {
      title = '收藏'
      this.setData({
        url: 'mini/v1/personal/my_collect'
      })
    } else {
      title = '我的报名'
      this.setData({
        url: 'mini/v1/personal/my_signup'
      })
    }

     wx.setNavigationBarTitle({
      title: title//页面标题为路由参数
    });

    this.list(1, 10, this.data.user_id)
  },
  onPullDownRefresh() {
    this.list(1, 10, this.data.user_id)
  },
  onReachBottom: function () {
      this.setData({
        page: this.data.page + 1
      })
       this.list(this.data.page, this.data.limit, this.data.user_id, 'more');
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //开关控制
    switchchoose:function(e){
      var curshow;
      if (e.currentTarget.dataset.index == this.data.curshow) {
        curshow = null;
      } else {
        curshow = e.currentTarget.dataset.index
      }

      this.setData({
        curshow: curshow
      })
    },
  //阻止冒泡
    stopcatch:function(e){
      if (this.data.curshow !== null){
        this.setData({
          curshow: null
        })
      }else{
        wx.navigateTo({
          url: '../needetile/needetile?id=' + e.currentTarget.dataset.id + '&&checkup=' + this.data.check
        })
      }
    },
  //取消报名
    signup_off:function(e){
      var title = e.currentTarget.dataset.type == 'signup' ? '报名' :'收藏';
      var that = this;
      http.request({
        url: e.currentTarget.dataset.type == 'signup' ? 'mini/v1/release/signup_off' : 'mini/v1/release/collect_off',
        data: { id: e.currentTarget.dataset.id },
        methods:'post',
        success: function (res) {
          wx.showToast({
            title: '已取消' + title+'！',
            icon: 'none',
            duration: 2000
          });
          var lists = that.data.datalist;
          var num = e.currentTarget.dataset.index;
          lists.splice(num, 1);
          that.setData({
            datalist: lists
          });
        }
      })
    },
  //删除发布需求
    demand_delete: function (e) {
      var that = this;
      var id = e.currentTarget.dataset.id;
      var num = e.currentTarget.dataset.index;
      var name = e.currentTarget.dataset.name;
      wx.showModal({
        title: '提示',
        content: '确定删除发布的' + name+'？ ',
        success(res) {
          if (res.confirm) {
            http.request({
              url: 'mini/v1/release/demand',
              data: { id: id },
              methods: 'delete',
              success: function (res) {
                var lists = that.data.datalist;
                lists.splice(num, 1);
                that.setData({
                  datalist: lists
                });
              }
            })
          } else if (res.cancel) {
          }
        }
      })
    },
  //我的发布状态更新
    closeopen:function(e){
      var that = this;
      var id = e.currentTarget.dataset.id;
      var status = e.currentTarget.dataset.status;
      var num = e.currentTarget.dataset.index;
      http.request({
        url: 'mini/v1/release/demand_status',
        data: { id: id, status: status =='1' ?'0' :'1'},
        methods: 'post',
        success: function (res) {
          that.setData({
            [`datalist[${num}].status`]: status == '1' ? '0' : '1'
          });
        }
      })
    }
})